package com.slibra.web.controller.business;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.slibra.business.domain.TXinyiDaily;
import com.slibra.business.domain.TXinyiIndustry;
import com.slibra.business.domain.TXinyiLaboratory;
import com.slibra.business.domain.TXinyiRobot;
import com.slibra.business.mapper.TXinyiDailyMapper;
import com.slibra.business.mapper.TXinyiIndustryMapper;
import com.slibra.business.mapper.TXinyiLaboratoryMapper;
import com.slibra.business.mapper.TXinyiRobotMapper;
import com.slibra.common.core.controller.BaseController;
import com.slibra.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 参数配置 信息操作处理
 * 
 *
 */
@Slf4j
@RestController
@RequestMapping("/test")
public class HandleDataController extends BaseController
{
    @Autowired
    private TXinyiIndustryMapper xinyiIndustryMapper;

    @Autowired
    private TXinyiDailyMapper xinyiDailyMapper;

    @Autowired
    private TXinyiLaboratoryMapper xinyiLaboratoryMapper;

    @Autowired
    private TXinyiRobotMapper xinyiRobotMapper;

    public static final String[] queryTags = {"信义污水厂JS_COD_Value","信义污水厂JS_PH_Value","信义污水厂JS_SS_Value","信义污水厂JS_ZL_Value","信义污水厂JS_ZA_Value","信义污水厂JS_AD_Value","信义污水厂JS_T_Value","信义污水厂进水泵房液位","信义污水厂出水瞬时流量","信义污水厂升级出水COD","信义污水厂升级出水PH","信义污水厂升级出水SS","信义污水厂升级出水TN","信义污水厂升级出水TP","信义污水厂升级出水氨氮","信义污水厂AIT202_Value","信义污水厂AIT203_Value","信义污水厂AIT207_Value","信义污水厂AIT206_Value","信义污水厂AIT209_Value","信义污水厂AIT210_Value","信义污水厂进水TDS","信义污水厂FT101_Value","信义污水厂SWCHHYHLB1_R_Value","信义污水厂SWCHHYHLB2_R_Value","信义污水厂SWCHHYHLB3_R_Value","信义污水厂SWCHHYHLB4_R_Value","信义污水厂SWCHHYHLB5_R_Value","信义污水厂SWCHHYHLB6_R_Value","信义污水厂SWCWNHLB1_R_Value","信义污水厂SWCWNHLB2_R_Value","信义污水厂SWCWNHLB3_R_Value","信义污水厂SWCWNHLB4_R_Value","信义污水厂SWCWNHLB5_R_Value","信义污水厂GFJ1_R_Value","信义污水厂GFJ2_R_Value","信义污水厂GFJ3_R_Value","信义污水厂GFJ4_R_Value","信义污水厂GFJ5_R_Value","信义污水厂GFJ6_R_Value","信义污水厂GFJ1_KQLL_Value","信义污水厂GFJ2_KQLL_Value","信义污水厂GFJ3_KQLL_Value","信义污水厂GFJ4_KQLL_Value","信义污水厂GFJ5_KQLL_Value","信义污水厂GFJ6_KQLL_Value"};

    @GetMapping("/dateAdd")
    public String dateAdd() {


        // 给定时间段的起始时间和结束时间
        LocalDateTime startTime = LocalDateTime.parse("2024-04-13T16:00:00");
        LocalDateTime endTime = LocalDateTime.parse("2024-04-13T17:00:00");


        /*LocalDateTime startTime = LocalDateTime.parse("2024-02-26T00:00:00");
        LocalDateTime endTime = LocalDateTime.parse("2024-02-27T00:00:00");*/

        // 每个小时的时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 循环按小时分割
        LocalDateTime currentHour = startTime;
        while (currentHour.isBefore(endTime)) {
            String begin = currentHour.format(formatter);
            String end = currentHour.plusMinutes(10).format(formatter);
            // 输出当前小时的起始时间和结束时间
            System.out.println("起始时间：" + begin);
            System.out.println("结束时间：" + end);

            // 当前小时加一小时，作为下一个小时的起始时间
            currentHour = currentHour.plusMinutes(10);

            //每个小时查询一次数据
            String url = "http://10.0.0.27:4568/api/v1/khquerydata";
            HashMap<String, Object> req = new HashMap<>();
            req.put("tagNames", queryTags);
            req.put("startTime", begin);
            req.put("endTime", end);
            req.put("recordNumbers", 100000);
            String body = HttpRequest.post(url).header("Authorization", "c2E6c2E=").header("clientName", "hongshan").body(JSON.toJSONString(req)).execute().body();
//            System.out.println("body = " + body);
            List<HashMap<String, String>> list = new ArrayList<>();

            //行转列数据处理
            for (String queryTag : queryTags) {
                JSONArray array = JSON.parseObject(body).getJSONArray(queryTag);
                //特殊数据处理一
                if(Objects.isNull(array) || array.isEmpty()){
                    System.out.println(queryTag + "查询到了空的数据，跳过本次循环");
                    continue;
                }
                int size = array.size();
                //特殊数据处理二
                if("0".equals(array.get(1) + "")){
                    System.out.println(queryTag + "查询到了数据，但是数据集合只有一条，且都是0");
                    continue;
                }
                //结合至少62个数据才满足条件(有可能获取不到)
                /*if(size < 62){
                    System.out.println(queryTag + "查询到了不符合条件的数据，跳过本次循环");
                    continue;
                }*/

                //存放的数据集
                //利用map去重
                HashMap<String, String> map = new LinkedHashMap<>();
                for (int i = 2; i < size; i++) {
//            System.out.println(i + "" + array.get(i));
                    JSONArray oneRecord = JSON.parseArray(JSON.toJSONString(array.get(i)));
                    //处理为空或者为0的数据
                    Object timeStampValue = oneRecord.get(2);
                    if(Objects.isNull(timeStampValue) || "0".equals(timeStampValue + ""))
                        continue;
                    BigDecimal value = Objects.isNull(oneRecord.get(0)) ? null : new BigDecimal(oneRecord.get(0) + "");
                    long timestamp = (long) timeStampValue;
                    String format = DateUtil.format(new Date(timestamp), DateUtils.YYYYMMDDHHMM_TS);
                    map.put(format, queryTag + "-" + value);
                }
                list.add(map);
            }
//        System.out.println("list = " + list);
            //先构造对象（list中任意一个map遍历的key个数）  因为合并完并不是1分钟60条，参考上面筛选区间，有时候会有几个值获取不到
            /*HashMap<String, String> oneRow = list.get(0);
            Set<String> set = oneRow.keySet();
            List<Industry> result = new ArrayList<>(set.size());
            set.forEach(s -> {
                Industry industry = new Industry();
                industry.setTestTime(s);
                result.add(industry);
            });*/
            Set<String> recordTimeSet = new HashSet<>();
            Map<String, TXinyiIndustry> recordMap = new HashMap<>();
            for (int i = 0; i < list.size(); i++) {
                HashMap<String, String> map = list.get(i);
                int finalJ = i;
                map.forEach((k, v) ->{
                    TXinyiIndustry industry = null;
                    if(!recordTimeSet.contains(k)){//第一次
                        industry = new TXinyiIndustry();
                        recordTimeSet.add(k);
                        recordMap.put(k, industry);
                    }else{
                        industry = recordMap.get(k);
                    }
                    industry.setTestTime(k);
                    //2024年4月15日11:19:52  额外增加2个字段
                    industry.setTestDate(k.substring(0,10));
                    industry.setTestHour(k.substring(0,13));
                    //解析值
                    String[] split = v.split("-");
                    String type = split[0];
                    BigDecimal value = new BigDecimal(split[1]);
                    if ("信义污水厂JS_COD_Value".equals(type)) {
                        industry.setJsCod(value);
                    } else if ("信义污水厂JS_PH_Value".equals(type)) {
                        industry.setJsPh(value);
                    } else if ("信义污水厂JS_SS_Value".equals(type)) {
                        industry.setJsSs(value);
                    } else if ("信义污水厂JS_ZL_Value".equals(type)) {
                        industry.setJsTp(value);
                    } else if ("信义污水厂JS_ZA_Value".equals(type)) {
                        industry.setJsTn(value);
                    } else if ("信义污水厂JS_AD_Value".equals(type)) {
                        industry.setJsNh3(value);
                    } else if ("信义污水厂JS_T_Value".equals(type)) {
                        industry.setJsSwPh(value);
                    } else if ("信义污水厂进水泵房液位".equals(type)) {
                        industry.setJsBfyw(value);
                    } else if ("信义污水厂出水瞬时流量".equals(type)) {
                        industry.setCsSlqc(value);
                    } else if ("信义污水厂升级出水COD".equals(type)) {
                        industry.setCsCod(value);
                    } else if ("信义污水厂升级出水PH".equals(type)) {
                        industry.setCsPh(value);
                    } else if ("信义污水厂升级出水SS".equals(type)) {
                        industry.setCsSs(value);
                    } else if ("信义污水厂升级出水TN".equals(type)) {
                        industry.setCsTn(value);
                    } else if ("信义污水厂升级出水TP".equals(type)) {
                        industry.setCsTp(value);
                    } else if ("信义污水厂升级出水氨氮".equals(type)) {
                        industry.setCsNh3(value);
                    } else if ("信义污水厂AIT202_Value".equals(type)) {
                        industry.setOneHyzdDo(value);
                    } else if ("信义污水厂AIT203_Value".equals(type)) {
                        industry.setOneHymdDo(value);
                    } else if ("信义污水厂AIT207_Value".equals(type)) {
                        industry.setTwoHyzdDo(value);
                    } else if ("信义污水厂AIT206_Value".equals(type)) {
                        industry.setTwoHymdDo(value);
                    } else if ("信义污水厂AIT209_Value".equals(type)) {
                        industry.setOneMlss(value);
                    } else if ("信义污水厂AIT210_Value".equals(type)) {
                        industry.setTwoMlss(value);
                    } else if ("信义污水厂进水TDS".equals(type)) {
                        industry.setJsTds(value);
                    } else if ("信义污水厂FT101_Value".equals(type)) {
                        industry.setJsSlq(value);
                    } else if ("信义污水厂SWCHHYHLB1_R_Value".equals(type)) {
                        industry.setNHlbOneGp(value);
                    } else if ("信义污水厂SWCHHYHLB2_R_Value".equals(type)) {
                        industry.setNHlbTwoGp(value);
                    } else if ("信义污水厂SWCHHYHLB3_R_Value".equals(type)) {
                        industry.setNHlbThreeGp(value);
                    } else if ("信义污水厂SWCHHYHLB4_R_Value".equals(type)) {
                        industry.setNHlbFourGp(value);
                    } else if ("信义污水厂SWCHHYHLB5_R_Value".equals(type)) {
                        industry.setNhlBFiveGp(value);
                    } else if ("信义污水厂SWCHHYHLB6_R_Value".equals(type)) {
                        industry.setNHlbSixGp(value);
                    } else if ("信义污水厂SWCWNHLB1_R_Value".equals(type)) {
                        industry.setWHlbOneGp(value);
                    } else if ("信义污水厂SWCWNHLB2_R_Value".equals(type)) {
                        industry.setWHlbTwoGp(value);
                    } else if ("信义污水厂SWCWNHLB3_R_Value".equals(type)) {
                        industry.setWHlbThreeGp(value);
                    } else if ("信义污水厂SWCWNHLB4_R_Value".equals(type)) {
                        industry.setWHlbFourGp(value);
                    } else if ("信义污水厂SWCWNHLB5_R_Value".equals(type)) {
                        industry.setWHlbFiveGp(value);
                    } else if ("信义污水厂GFJ1_R_Value".equals(type)) {
                        industry.setFjOne(value);
                    } else if ("信义污水厂GFJ2_R_Value".equals(type)) {
                        industry.setFjTwo(value);
                    } else if ("信义污水厂GFJ3_R_Value".equals(type)) {
                        industry.setFjThree(value);
                    } else if ("信义污水厂GFJ4_R_Value".equals(type)) {
                        industry.setFjFour(value);
                    } else if ("信义污水厂GFJ5_R_Value".equals(type)) {
                        industry.setFjFive(value);
                    } else if ("信义污水厂GFJ6_R_Value".equals(type)) {
                        industry.setFjSix(value);
                    } else if ("信义污水厂GFJ1_KQLL_Value".equals(type)) {
                        industry.setKqllOne(value);
                    } else if ("信义污水厂GFJ2_KQLL_Value".equals(type)) {
                        industry.setKqllTwo(value);
                    } else if ("信义污水厂GFJ3_KQLL_Value".equals(type)) {
                        industry.setKqllThree(value);
                    } else if ("信义污水厂GFJ4_KQLL_Value".equals(type)) {
                        industry.setKqllFour(value);
                    } else if ("信义污水厂GFJ5_KQLL_Value".equals(type)) {
                        industry.setKqllFive(value);
                    } else if ("信义污水厂GFJ6_KQLL_Value".equals(type)) {
                        industry.setKqllSix(value);
                    }else if ("信义污水厂实际碳源加药量".equals(type)) {
                        industry.setSJTYJLY(value);
                    }else if ("信义污水厂除磷加药瞬时流量".equals(type)) {
                        industry.setCLJYSSLL(value);
                    }
                    //只有最后一次才执行数据库添加
                    if(finalJ == list.size()-1){
                        //插入数据库
                        xinyiIndustryMapper.insertTXinyiIndustry(industry);
                    }
                });




            }

        }
        return "ok";
    }

//    @GetMapping("/dateUpdateCL")
//    public String dateUpdateCL() {
//        String fileName = "除磷加药瞬时流量";
//        //输入目录
//        ExcelReader reader = ExcelUtil.getReader("C:\\Users\\10109\\Desktop\\新程序\\" + fileName + ".xls");
//        //解析数据
//        List<List<Object>> readAll = reader.read();
//        if(!CollectionUtils.isEmpty(readAll)){
//            for (List<Object> objects : readAll) {
//                String time = "20" + objects.get(0) + " " + objects.get(1).toString().substring(0, 5);
//                System.out.println("time = " + time);
//                TXinyiIndustry uniqueTXinyiIndustry = this.xinyiIndustryMapper.getUniqueTXinyiIndustry(time);
//                if(!Objects.isNull(uniqueTXinyiIndustry)){
//                    System.out.println(objects.get(2));
//                    BigDecimal CLJYSSLL = Objects.isNull(objects.get(2)) ? null : new BigDecimal(String.valueOf(objects.get(2)));
//                    uniqueTXinyiIndustry.setCLJYSSLL(CLJYSSLL);
//                    this.xinyiIndustryMapper.updateTXinyiIndustry(uniqueTXinyiIndustry);
//                }
//            }
//        }
//        return "ok";
//    }
//
//
//    @GetMapping("/dateUpdateSJTY")
//    public String dateUpdateSJTY() {
//        String fileName = "信义污水厂实际碳源加药量";
//        //输入目录
//        ExcelReader reader = ExcelUtil.getReader("C:\\Users\\10109\\Desktop\\新程序\\" + fileName + ".xls");
//        //解析数据
//        List<List<Object>> readAll = reader.read();
//        if(!CollectionUtils.isEmpty(readAll)){
//            int i = 0;
//            for (List<Object> objects : readAll) {
//                String format = "";
//                if(objects.get(0) instanceof String){
//                    System.out.println("----------是字符串类型的");
//                    String time = ((String) objects.get(0));
//                    System.out.println("time = " + time);
//                    format =  time.replaceAll("-", "/").substring(0, 16);
//                }else if (objects.get(0) instanceof cn.hutool.core.date.DateTime){
//                    System.out.println("++++++++++是日期类型的");
//                    cn.hutool.core.date.DateTime time = (DateTime) objects.get(0);
//                    System.out.println("time = " + time);
//                    format = DateUtil.format(time, DateUtils.YYYYMMDDHHMM_TS);
//                    System.out.println("format = " + format);
//                }else {
//                    System.out.println(JSON.toJSONString(objects));
//                    format = "";
//                    i++;
//                }
//                TXinyiIndustry uniqueTXinyiIndustry = this.xinyiIndustryMapper.getUniqueTXinyiIndustry(format);
//                if(!Objects.isNull(uniqueTXinyiIndustry)){
//                    BigDecimal SJTYJLY = Objects.isNull(objects.get(1)) ? null : new BigDecimal(String.valueOf(objects.get(1)));
//                    uniqueTXinyiIndustry.setSJTYJLY(SJTYJLY);
//                    this.xinyiIndustryMapper.updateTXinyiIndustry(uniqueTXinyiIndustry);
//                }
//            }
//            System.out.println("i =" + i);
//        }
//        return "ok";
//    }
//
//    //下面是新的 将一个小时的数据 更新到一个小时之内的所有的数据中
//    @GetMapping("/dateUpdateCLMinute")
//    public String dateUpdateCLMinute() {
//        String fileName = "除磷加药瞬时流量";
//        //输入目录
//        ExcelReader reader = ExcelUtil.getReader("C:\\Users\\10109\\Desktop\\新程序\\" + fileName + ".xls");
//        //解析数据
//        List<List<Object>> readAll = reader.read();
//        int i = 0;
//        if(!CollectionUtils.isEmpty(readAll)){
//            for (List<Object> objects : readAll) {
//                if(!Objects.isNull(objects.get(0)) && !Objects.isNull(objects.get(1))){
//                    String beginTime = "20" + objects.get(0) + " " + objects.get(1).toString().substring(0, 5);
//                    System.out.println("beginTime = " + beginTime);
//                    String endTime = "20" + objects.get(0) + " " + objects.get(1).toString().substring(0, 3) + "59";
//                    System.out.println("endTime = " + endTime);
//                    //更新数据
//                    System.out.println(objects.get(2));
//                    //有特殊情况
//                    if(Objects.isNull(objects.get(2)) || objects.get(2).toString().contains("-")){
//                        System.out.println("存在特殊的情况，值包含-,不做更新");
//                        i++;
//                    }else{
//                    this.xinyiIndustryMapper.updateTXinyiIndustryCLByBetweenTime(beginTime, endTime, new BigDecimal(String.valueOf(objects.get(2))));
//                    }
//                }
//            }
//        }
//        System.out.println("i = " + i);
//        return "ok";
//    }
//
//
//    @GetMapping("/dateUpdateSJTYMinute")
//    public String dateUpdateSJTYMinute() {
//        String fileName = "信义污水厂实际碳源加药量";
//        //输入目录
//        ExcelReader reader = ExcelUtil.getReader("C:\\Users\\10109\\Desktop\\新程序\\" + fileName + ".xls");
//        //解析数据
//        List<List<Object>> readAll = reader.read();
//        if(!CollectionUtils.isEmpty(readAll)){
//            int i = 0;
//            for (List<Object> objects : readAll) {
//                String beginTime = "";
//                if(objects.get(0) instanceof String){
//                    System.out.println("----------是字符串类型的");
//                    String time = ((String) objects.get(0));
////                    System.out.println("time = " + time);
//                    beginTime = time.replaceAll("-", "/").substring(0, 16);
//                }else if (objects.get(0) instanceof cn.hutool.core.date.DateTime){
//                    System.out.println("++++++++++是日期类型的");
//                    cn.hutool.core.date.DateTime time = (DateTime) objects.get(0);
////                    System.out.println("time = " + time);
//                    beginTime = DateUtil.format(time, DateUtils.YYYYMMDDHHMM_TS);
//                    System.out.println("beginTime = " + beginTime);
//                }else {
//                    System.out.println(JSON.toJSONString(objects));
//                    beginTime = "";
//                    i++;
//                }
//                //endTime
//                String endTime = beginTime.substring(0, 14) + "59";
//                System.out.println("endTime = " + endTime);
//                //更新数据
//                BigDecimal sjtyjly = Objects.isNull(objects.get(1)) ? null : new BigDecimal(String.valueOf(objects.get(1)));
//                this.xinyiIndustryMapper.updateTXinyiIndustrySJByBetweenTime(beginTime, endTime, sjtyjly);
//            }
//            System.out.println("i =" + i);
//        }
//        return "ok";
//    }





//    @GetMapping("/dateUpdateSJTYMinute")
//    public String dateUpdateSJTYMinute() {
//        log.info("进入了手动处理两个值的操作~~~~~~~~");
//        File file = new File("/home/slibra/历史数据");
//        if(file.isDirectory()){
//            for (File listFile : file.listFiles()) {
//                //        输入目录
//                ExcelReader reader = ExcelUtil.getReader(listFile);
//                List<List<Object>> readAll = reader.read();
//                if(!CollectionUtils.isEmpty(readAll)){
//                    for (int i = 3; i < readAll.size(); i++) {
//                        //解析数据
//                        List<Object> objects = readAll.get(i);
//                        String time = "20" + objects.get(0).toString().replaceAll(" ", "") + " " + objects.get(1).toString().replaceAll(" ", "").substring(0, 5);
////                        System.out.println("time = " + time);
//                        TXinyiIndustry uniqueTXinyiIndustry = this.xinyiIndustryMapper.getUniqueTXinyiIndustry(time);
//                        if(!Objects.isNull(uniqueTXinyiIndustry)){
//                            TXinyiIndustry updateBean = new TXinyiIndustry();
//                            updateBean.setID(uniqueTXinyiIndustry.getID());
//                            BigDecimal CLJYSSLL = Objects.isNull(objects.get(2)) || String.valueOf(objects.get(2)).contains("-") ? null : new BigDecimal(String.valueOf(objects.get(2)));
//                            updateBean.setCLJYSSLL(CLJYSSLL);
//                            BigDecimal SJTYJLY = Objects.isNull(objects.get(3)) || String.valueOf(objects.get(3)).contains("-") ? null : new BigDecimal(String.valueOf(objects.get(3)));
//                            updateBean.setSJTYJLY(SJTYJLY);
//                            //如果两个都不为空再更新
//                            if(!Objects.isNull(CLJYSSLL) || !Objects.isNull(SJTYJLY)){
//                                this.xinyiIndustryMapper.updateTXinyiIndustry(updateBean);
//                            }else{
//                                log.error("要更新的两个字段全部为空--------");
//                            }
//                        }
//                    }
//                }
//                }
//            }
//        return "ok";
//    }


    /**
     * 手动处理日报数据 增加两个字段 时间和小时
     * @return
     */
    @GetMapping("/dateUpdateDaily")
    public String dateUpdateDaily() {
        //获取全部数据
        List<TXinyiDaily> tXinyiDailies = this.xinyiDailyMapper.selectTXinyiDailyList(null);
        if(!CollectionUtils.isEmpty(tXinyiDailies)){
            for (TXinyiDaily tXinyiDaily : tXinyiDailies) {
                String handleTime = handleDate(tXinyiDaily.getTestTime());
                tXinyiDaily.setTestTime(handleTime);
                tXinyiDaily.setTestDate(handleTime);
                //更新
                this.xinyiDailyMapper.updateTXinyiDaily(tXinyiDaily);
            }
        }
        return "ok";
    }


    /**
     * 手动处理化验数据 增加两个字段 时间和小时
     * @return
     */
    @GetMapping("/dateUpdateHuaYan")
    public String dateUpdateHuaYan() {
        //获取全部数据
        List<TXinyiLaboratory> tXinyiLaboratories = this.xinyiLaboratoryMapper.selectTXinyiLaboratoryList(null);
        if(!CollectionUtils.isEmpty(tXinyiLaboratories)){
            for (TXinyiLaboratory tXinyiLaboratory : tXinyiLaboratories) {
                String handleTime = handleDate(tXinyiLaboratory.getTestTime());
                tXinyiLaboratory.setTestTime(handleTime);//到分为止
                tXinyiLaboratory.setTestDate(handleTime.substring(0, 10));
                tXinyiLaboratory.setTestHour(handleTime.substring(0, 13));
                //更新
                this.xinyiLaboratoryMapper.updateTXinyiLaboratory(tXinyiLaboratory);
            }
        }
        return "ok";
    }



    /**
     * 手动处理 工业库的 某个值，将历史数据全部都更新
     * @return
     */
    @GetMapping("/updateGYKSomeFields")
    public String updateGYKSomeFields() {
        //更新的某个指标
        String[] queryTags = {"信义污水厂_除磷P04预测值_"};
        //找到对应的数据，然后更新它
        // 给定时间段的起始时间和结束时间
        LocalDateTime startTime = LocalDateTime.parse("2022-07-01T00:00:00");
        LocalDateTime endTime = LocalDateTime.parse("2024-04-25T09:00:01");

        // 每个小时的时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 循环按小时分割
        LocalDateTime currentHour = startTime;
        while (currentHour.isBefore(endTime)) {
            String begin = currentHour.format(formatter);
            String end = currentHour.plusDays(1).format(formatter);
            // 输出当前小时的起始时间和结束时间
            System.out.println("起始时间：" + begin);
            System.out.println("结束时间：" + end);

            // 当前小时加一小时，作为下一个小时的起始时间
            currentHour = currentHour.plusDays(1);

            //每个小时查询一次数据
            String url = "http://10.0.0.27:4568/api/v1/khquerydata";
            HashMap<String, Object> req = new HashMap<>();
            req.put("tagNames", queryTags);
            req.put("startTime", begin);
            req.put("endTime", end);
            req.put("recordNumbers", 100000);
            String body = HttpRequest.post(url).header("Authorization", "c2E6c2E=").header("clientName", "hongshan").body(JSON.toJSONString(req)).execute().body();
//            System.out.println("body = " + body);
            List<HashMap<String, String>> list = new ArrayList<>();

            //行转列数据处理
            for (String queryTag : queryTags) {
                JSONArray array = JSON.parseObject(body).getJSONArray(queryTag);
                //特殊数据处理一
                if(Objects.isNull(array) || array.isEmpty()){
                    System.out.println(queryTag + "查询到了空的数据，跳过本次循环");
                    continue;
                }
                int size = array.size();
                //特殊数据处理二
                if("0".equals(array.get(1) + "")){
                    System.out.println(queryTag + "查询到了数据，但是数据集合只有一条，且都是0");
                    continue;
                }

                //存放的数据集
                //利用map去重
                HashMap<String, String> map = new LinkedHashMap<>();
                for (int i = 2; i < size; i++) {
//            System.out.println(i + "" + array.get(i));
                    JSONArray oneRecord = JSON.parseArray(JSON.toJSONString(array.get(i)));
                    //处理为空或者为0的数据
                    Object timeStampValue = oneRecord.get(2);
                    if(Objects.isNull(timeStampValue) || "0".equals(timeStampValue + ""))
                        continue;
                    BigDecimal value = Objects.isNull(oneRecord.get(0)) ? null : new BigDecimal(oneRecord.get(0) + "");
                    long timestamp = (long) timeStampValue;
                    String format = DateUtil.format(new Date(timestamp), DateUtils.YYYYMMDDHHMM_TS);
                    map.put(format, queryTag + "-" + value);
                }
                list.add(map);
            }
            Set<String> recordTimeSet = new HashSet<>();
            Map<String, TXinyiIndustry> recordMap = new HashMap<>();
            for (int i = 0; i < list.size(); i++) {
                HashMap<String, String> map = list.get(i);
                int finalJ = i;
                map.forEach((k, v) ->{
                    TXinyiIndustry industry = null;
                    if(!recordTimeSet.contains(k)){//第一次
                        industry = new TXinyiIndustry();
                        recordTimeSet.add(k);
                        recordMap.put(k, industry);
                    }else{
                        industry = recordMap.get(k);
                    }
                    industry.setTestTime(k);
                    //解析值
                    String[] split = v.split("-");
                    String type = split[0];
                    BigDecimal value = new BigDecimal(split[1]);
                    if ("信义污水厂_除磷P04预测值_".equals(type)) {
                        industry.setCLP04YCZ(value);//目前值更新此字段
                    }
                    /*else if ("信义污水厂除磷加药瞬时流量".equals(type)) {
                        industry.setCLJYSSLL(value);
                    }*/

                    //只有最后一次才执行数据库添加
                    if(finalJ == list.size()-1){
                        //先去查询数据库
                        TXinyiIndustry uniqueTXinyiIndustry = this.xinyiIndustryMapper.getUniqueTXinyiIndustry(industry.getTestTime());
                        if(Objects.isNull(uniqueTXinyiIndustry))
                            log.error("查询到的工业库数据为null，化验时间为{}", industry.getTestTime());
                        else {
                            //防止其他信息被更新，新建对象
                            TXinyiIndustry updateTXinyiIndustry = new TXinyiIndustry();
                            //赋值
                            updateTXinyiIndustry.setID(uniqueTXinyiIndustry.getID());
                            updateTXinyiIndustry.setCLP04YCZ(industry.getCLP04YCZ());
                            //更新数据库
                            xinyiIndustryMapper.updateTXinyiIndustry(updateTXinyiIndustry);
                        }
                    }
                });

            }
        }
        return "ok";
    }



    public static void main(String[] args) {
//        File file = new File("C:\\Users\\10109\\Desktop\\新程序\\历史数据");
//        if(file.isDirectory()){
//            for (File listFile : file.listFiles()) {
////                System.out.println(listFile.getName());
//                ExcelReader reader = ExcelUtil.getReader(listFile);
//                List<List<Object>> readAll = reader.read();
//                for (int i = 3; i < 10; i++) {
//                    System.out.println(readAll.get(i));
//                }
//                System.out.println("------------------");
//            }
//        }


        String str = "2022/07/11 19:00";
//        String str = "2022/7/1";
//        String str = "0:0:1";
//        String newStr = handleDate(str);
//        System.out.println(newStr);
//        System.out.println(str.substring(0, 12));
//        System.out.println(str.substring(0, 9));

//        System.out.println(handleDate("2024-3-9"));
        System.out.println(handleDate("2023/12/26 02:00"));
        System.out.println(handleDate("2023/12/26 02:00").substring(0, 10));
        System.out.println(handleDate("2023/12/26 02:00").substring(0, 13));

    }


    public static String handleDate(String str){
        StringBuilder sb = new StringBuilder();
        if(str.contains(" ")){//包含空格 就是年月日时分秒了
            String[] split = str.split(" ");
            addBeforeZero(sb, split[0], "/");
            sb.append(" ");
            addBeforeZero(sb, split[1], ":");
        }else{
            if(str.contains("/")){//年月日
                addBeforeZero(sb, str, "/");
            }else if(str.contains(":")){//时分秒
                addBeforeZero(sb, str, ":");
            }else if(str.contains("-")){//只有年月日
                addBeforeZero(sb, str, "-");
            }else {
                sb.append(str);
            }
       }
        return sb.toString();
    }

    public static StringBuilder addBeforeZero(StringBuilder sb, String str, String tag){
        String[] split = str.split(tag);
        int length = split.length;
        for (int i = 0; i < length; i++) {
            String value = split[i];
            Integer intValue = Integer.parseInt(value);
            if(intValue < 10 && value.length() == 1){////防止有正确的情况 额外再补充字符串
                sb.append(0).append(value);
            }else{
                sb.append(value);
            }
            if(i < length-1){
                sb.append(tag);
            }
        }
        return sb;
    }

}
